package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scm.entity.ScmIndustryType;
import org.jeecg.modules.scm.service.IScmIndustryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

 /**
 * @Description: scm_industry_type
 * @Author: jeecg-boot
 * @Date:   2021-10-28
 * @Version: V1.0
 */
@Api(tags="行业分类")
@RestController
@RequestMapping("/industry_type/scmIndustryType")
@Slf4j
public class ScmIndustryTypeController extends JeecgController<ScmIndustryType, IScmIndustryTypeService> {
	@Autowired
	private IScmIndustryTypeService scmIndustryTypeService;

	/**
	 * 分页列表查询
	 *
	 * @param scmIndustryType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-分页列表查询")
	@ApiOperation(value="scm_industry_type-分页列表查询", notes="scm_industry_type-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScmIndustryType scmIndustryType,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ScmIndustryType> queryWrapper = QueryGenerator.initQueryWrapper(scmIndustryType, req.getParameterMap());
		Page<ScmIndustryType> page = new Page<ScmIndustryType>(pageNo, pageSize);
		IPage<ScmIndustryType> pageList = scmIndustryTypeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param scmIndustryType
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-添加")
	@ApiOperation(value="scm_industry_type-添加", notes="scm_industry_type-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ScmIndustryType scmIndustryType) {
		scmIndustryTypeService.save(scmIndustryType);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param scmIndustryType
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-编辑")
	@ApiOperation(value="scm_industry_type-编辑", notes="scm_industry_type-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ScmIndustryType scmIndustryType) {
		scmIndustryTypeService.updateById(scmIndustryType);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-通过id删除")
	@ApiOperation(value="scm_industry_type-通过id删除", notes="scm_industry_type-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		scmIndustryTypeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-批量删除")
	@ApiOperation(value="scm_industry_type-批量删除", notes="scm_industry_type-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.scmIndustryTypeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_industry_type-通过id查询")
	@ApiOperation(value="scm_industry_type-通过id查询", notes="scm_industry_type-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ScmIndustryType scmIndustryType = scmIndustryTypeService.getById(id);
		if(scmIndustryType==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(scmIndustryType);
	}

	 /**
	  * 查询行业分类
	  *
	  * @return
	  */
	 @AutoLog(value = "scm_industry_type-查询行业分类")
	 @ApiOperation(value="scm_industry_type-查询行业分类", notes="scm_industry_type-查询行业分类")
	 @GetMapping(value = "/queryIndustryType")
	 public Result<?> queryIndustryType() {

		 QueryWrapper<ScmIndustryType> queryWrapper = new QueryWrapper();
		 queryWrapper.select("distinct industry_type,type_name");
		 List<ScmIndustryType> list = scmIndustryTypeService.list(queryWrapper);
		 if(list==null){
			 return Result.error("查询失败");
		 }
		 Map<String,List> res = new HashMap<>();
		 List<String> list_type = new LinkedList<>();
		 List<String> list_name = new LinkedList<>();
		 for(ScmIndustryType scmIndustryType:list){
		 	list_type.add(scmIndustryType.getIndustryType());
		 	list_name.add(scmIndustryType.getTypeName());
		 }
		 res.put("industry_type",list_type);
		 res.put("type_name",list_name);

//		 System.out.println(list);

		 return Result.OK(res);
	 }

	 /***
	  * 模糊查询行业名称
	  * */
	 @AutoLog(value = "模糊查询行业名称")
	 @ApiOperation(value="模糊查询行业名称", notes="模糊查询行业名称")
	 @GetMapping(value = "/fuzzyQueryIndustry")
	 public Result<?> fuzzyQueryIndustry(@RequestParam(name="industryName",required = false) String industryName){
		 List<ScmIndustryType> scmIndustryTypes = scmIndustryTypeService.fuzzyQueryIndustry(industryName);
		 return Result.OK(scmIndustryTypes);
	 }

 }
